
package cz.muni.fi.PA165.musiclibrarybackend.dao;

import cz.muni.fi.PA165.musiclibrarybackend.entities.Genre;
import cz.muni.fi.PA165.musiclibrarybackend.entities.IllegalEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Jakub Gavornik, 325377
 */
@Repository
public class GenreDAOImpl implements GenreDAO, Serializable{
    
    @PersistenceContext
    private EntityManager entityManager;
    
    public EntityManager getEntityManager() {
        return entityManager;
    }

    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }
    
    public void createGenre(Genre genre) {
        if (genre == null) {
            throw new IllegalArgumentException("genre is null");
        }
        
        if (genre.getId() != null) {
            throw new IllegalEntityException("genre id is already set");                    
        }
        
        if (genre.getName() == null) {
            throw new IllegalArgumentException("name has to be set");
        }
        
        entityManager.persist(genre);        
    }

    public Genre getGenre(Long id) {
        
        Genre genre;        
        if (id == null) {
            throw new IllegalArgumentException("id is null");            
        }        
        
        genre = entityManager.find(Genre.class, id);
        return genre;
    }

    public void updateGenre(Genre genre) {
        if (genre == null || genre.getId() == null || genre.getName() == null) {
            throw new IllegalArgumentException();
        }        
        Genre genre2 = entityManager.find(Genre.class, genre.getId());
        if (genre2 == null){
            throw new IllegalArgumentException();
        }
        genre2.setName(genre.getName());
        entityManager.persist(genre2);
    }

    public void deleteGenre(Genre genre) {
        if (genre == null || genre.getId() == null) {
            throw new IllegalArgumentException("genre is null");
        }
        Genre genre2 = entityManager.find(Genre.class, genre.getId());
        
        if (genre2 == null) {
            throw new IllegalArgumentException("trying to delete genre which doesnt exists");
        }
        
        if (!(genre2.getId().equals(genre.getId())) 
             || !(genre2.getName().equals(genre.getName()))) {
            
            throw new IllegalArgumentException("trying to delete wrong genre");
        }
        entityManager.remove(genre2);
    }

    public List<Genre> getAllGenres() {
        List<Genre> genres;
        Query query = entityManager.createQuery("SELECT genre FROM Genre genre");
        genres = (List<Genre>) query.getResultList();
        return genres;
        
    }
    
}
